<template>
	<view class="fixed-bottom u-p-80" style="bottom:50px">
		<!-- #ifdef H5 -->
		<u-button @click="getTestToken" type="primary" shape="circle">获取测试token</u-button>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
<!-- 		<u-button @click="getTestToken" type="primary" shape="circle">获取测试token</u-button>
		<u-gap height="10"></u-gap> -->
		<u-button open-type="getPhoneNumber" @getphonenumber="getphonenumber" type="primary"
			shape="circle">授权手机号</u-button>
		<!-- <button type="default" size="mini"  style="color:#ffffff;backgroundColor:#259DFF;borderColor:#259DFF" hover-class="is-hover" open-type="getPhoneNumber" @getphonenumber="getphonenumber">授权手机号</button> -->
		<!-- #endif -->
	</view>
</template>

<script setup>
	import store from '@/store/index.js'; //需要引入store
	import mixins from '@/common/js/mixins.js';
	import {
		ref,
		reactive,
		computed,
		getCurrentInstance
	} from 'vue'
	import {
		onReady,
		onLoad,
		onShow
	} from '@dcloudio/uni-app'
	const {
		proxy,
	} = getCurrentInstance()
	const getphonenumber = (e) => {
		console.log(e.detail.code) // 动态令牌
		console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
		console.log(e.detail.errno) // 错误码（失败时返回）
		uni.showLoading({
			title: '加载中'
		});
		proxy.$u.api.userGetPhone({
			code: e.detail.code
		}).then(res => {
			let {
				data,
				code,
				msg
			} = res;
			if (code == 200) {
				login(data.tel)
			} else {
				uni.$u.toast(msg || '错误');
			}
		})
	}
	const login = async (tel) => {
		let {
			code
		} = await wxlogin();
		proxy.$u.api.userLogin({
			code: code,
			tel: tel
		}).then(res => {
			let {
				data,
				code,
				msg
			} = res;
			if (code == 200) {
				store.commit('set_token', data.token)
				store.dispatch('getUserInfo')
			} else {
				uni.$u.toast(msg || '错误');
			}
		})
	}
	
	const wxlogin = () => {
		return new Promise(function(resolve, reject) {
			wx.login({
				success: function(res) {
					console.log(res);
					if (res.code) {
						resolve(res);
					} else {
						reject(res);
					}
				},
				fail: function(err) {
					reject(err);
				}
			});
		});
	}
	const getTestToken = () => {
		uni.showLoading({
			title: '加载中'
		});
		proxy.$u.api.getTestToken({
			user_id: 11909 //16922
		}).then(res => {
			let {
				data,
				code,
				msg
			} = res;
			if (code == 200) {
				store.commit('set_token', data.token)
				store.dispatch('getUserInfo')
			} else {
				uni.$u.toast(msg || '错误');
			}
		})
	}
</script>

<style>

</style>